﻿using System;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Web.Framework.Controls;
using iLib.Web.UI;

namespace Cms.Web.UI.Admin
{
    public partial class CustomizeEditor : AdminPage, ICustomizeEditorView
    {
        private CustomizeEditorPresenter _presenter;

        public string CurrentTab
        {
            get { return tabStrip.CurrentValue; }
        }

        public string ContextTranslationFile
        {
            get { return "Admin/Translations/CustomizeEditor.xml"; }
        }

        public string InitJavascript
        {
            get { return hidInitJavascript.Value; }
            set { hidInitJavascript.Value = value; }
        }

        public string CustomJson
        {
            get { return hidCustomJson.Value; }
            set { hidCustomJson.Value = value; }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new CustomizeEditorPresenter(this);
            if (Page.IsPostBack)
            {
                return;
            }
            tabStrip.SelectedValue = Request.QueryString["tab"];
            _presenter.Initialize();
            HeadingIcon = litIcon.Text;
            HeadingText = GetText(this, "Heading");
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            tiInitJavascript.OnClientClick = GetClientClickScript(tiInitJavascript);
            tiCustomJson.OnClientClick = GetClientClickScript(tiCustomJson);
        }

        protected override void OnPreRenderComplete(EventArgs e)
        {
            base.OnPreRenderComplete(e);
            ClientScriptProxy.Current.RegisterStartupScript(
                tabStrip,
                GetType(),
                "updatePanels",
                "updatePanels('" + tabStrip.SelectedValue + "');",
                true);
        }

        protected void tabStrip_Click(string value)
        {
            if (CurrentTab == "initJavascript")
            {
                if (hidSaveInitJavascript.Value == "ok")
                {
                    _presenter.Save();
                }
                else
                {
                    _presenter.Initialize();
                }
            }
            if (CurrentTab == "customJson")
            {
                if (hidSaveCustomJson.Value == "ok")
                {
                    _presenter.Save();
                }
                else
                {
                    _presenter.Initialize();
                }
            }
            if (_presenter.IsValid)
            {
                ClientScriptProxy.Current.RegisterStartupScript(tabStrip, GetType(), "init", "init();", true);
            }
            upnlInitJavascript.Update();
            upnlCustomJson.Update();
            upnlTabs.Update();
        }

        protected void lbSave_Click(object sender, EventArgs e)
        {
            _presenter.Save();
        }

        private string GetClientClickScript(AdminTabItem ti)
        {
            return "cms.Edit.ShowTabOverlays(); " +
                   "updateCode(); " +
                   "if (isDirty()) {" +
                        "return cms.Dialogs.Confirm(" +
                            "function (status) { " +
                                "document.getElementById('ctl00_body_hidSaveInitJavascript').value = status;" +
                                "document.getElementById('ctl00_body_hidSaveCustomJson').value = status;" +
                                "document.getElementById('ctl00_tabs_hidSave').value = status;" +
                                "__doPostBack('" + ti.UniqueID + "$" + ti.ID + "','');" +
                            "}, " +
                           "'" + GetText(this, "SaveInfoHeader") + "', " +
                           "'" + GetText(this, "OptionalSaveInfoMessage") + "', " +
                           "'" + GetText(this, "SaveInfoOK") + "', " +
                           "'" + GetText(this, "SaveInfoCancel") + "', " +
                           "true, " +
                           "null" +
                       ");" +
                   "}";
        }
    }
}